package imageManager.service;


import imageManager.image.*;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import javax.imageio.ImageIO;
import javax.jws.soap.SOAPBinding;
import javax.jws.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.SchemaFactory;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.MTOM;

import org.xml.sax.SAXException;


@WebService(name = "ImageManager", targetNamespace = "imageManager.image")
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL)
public class ImageManager {
	private ObjectFactory facto = new ObjectFactory();
	private String serverID;
	private int charge;
	private Hashtable<String,Boolean> pool;
	
	public ImageManager() {
		charge = 0;
		serverID = "Server" + Double.toString(Math.random()*100);
		pool = new Hashtable<String, Boolean>(10);
		initServer(10);
	}
	
	public ImageManager(int chargeMax, String serverID) {
		this.serverID = serverID;
		pool = new Hashtable<String,Boolean>(chargeMax);
		charge = 0;
		initServer(chargeMax);
	}
	
	private void initServer(int chargeMax) {
		//On crée le pool d'ID
		//Les IDs sont de la forme "<serverID>-client<clientNum>"
		for(int i = 0; i < chargeMax; i++) {
			pool.put(serverID + "-client" + Integer.toString(i), new Boolean(false));
		}
	}
	
	@WebMethod(operationName = "CreateImage")
	@WebResult(name = "reussi")
	public boolean createImage(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,			
			@WebParam(name = "width", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int width,
			@WebParam(name = "height", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int height) throws FileAlreadyExistsException {
		
		File file = new File(nom + ".xml");
		if(file.exists()) {
			throw new FileAlreadyExistsException("Désolé mais le fichier " + nom + ".xml existe déjà.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
			MetaImage image = facto.createMetaImage(nom, width, height);
			
			marsh.marshal(image, new File(nom + ".xml"));
		}
		catch (JAXBException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "DeleteImage")
	@WebResult(name = "reussi")
	public boolean deleteImage(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}

		file.delete();
		return true;
	}
	
	@WebMethod(operationName = "AddPoint")
	@WebResult(name = "reussi")
	public boolean addPoint(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

			//On trouve l'image et on effectue l'unmarshal
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			//On cr�e et on ajoute l'objet
			Point point = facto.createPoint(x, y, r, g, b);
			image.getObjets().add(point);
			
			//On marshal l'image modifi�e
			marsh.marshal(image, new File(nom + ".xml"));
			
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "AddRectangle")
	@WebResult(name = "reussi")
	public boolean addRectangle(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			boolean fill,
			@WebParam(name = "longueur", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int longueur,
			@WebParam(name = "largeur", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int largeur) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

			//On trouve l'image et on effectue l'unmarshal
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			//On cr�e et on ajoute l'objet
			Rectangle rect = facto.createRectangle(x, y, r, g, b, r2, g2, b2, fill, longueur, largeur);
			image.getObjets().add(rect);
			
			//On marshal l'image modifi�e
			marsh.marshal(image, new File(nom + ".xml"));
			
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "AddArc")
	@WebResult(name = "reussi")
	public boolean addArc(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			boolean fill,
			@WebParam(name = "longueur", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int longueur,
			@WebParam(name = "largeur", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int largeur,
			@WebParam(name = "AngleDebut", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int angleDebut,
			@WebParam(name = "AngleFin", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int angleFin) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

			//On trouve l'image et on effectue l'unmarshal
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			//On cr�e et on ajoute l'objet
			Arc arc = facto.createArc(x, y, r, g, b, r2, g2, b2, fill, longueur, largeur, angleDebut, angleFin);
			image.getObjets().add(arc);
			
			//On marshal l'image modifi�e
			marsh.marshal(image, new File(nom + ".xml"));
			
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "AddEllipse")
	@WebResult(name = "reussi")
	public boolean addEllipse(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			boolean fill,
			@WebParam(name = "longueur", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int longueur,
			@WebParam(name = "largeur", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int largeur) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

			//On trouve l'image et on effectue l'unmarshal
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			//On cr�e et on ajoute l'objet
			Ellipse ellipse = facto.createEllipse(x, y, r, g, b, r2, g2, b2, fill, longueur, largeur);
			image.getObjets().add(ellipse);
			
			//On marshal l'image modifi�e
			marsh.marshal(image, new File(nom + ".xml"));
			
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "AddLigne")
	@WebResult(name = "reussi")
	public boolean addLigne(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b,
			@WebParam(name = "x2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x2,
			@WebParam(name = "y2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y2) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

			//On trouve l'image et on effectue l'unmarshal
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			//On cr�e et on ajoute l'objet$
			Point point = facto.createPoint(x2, y2, r, g, b);
			Ligne ligne = facto.createLigne(x, y, r, g, b, point);
			image.getObjets().add(ligne);
			
			//On marshal l'image modifi�e
			marsh.marshal(image, new File(nom + ".xml"));
			
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "AddPolygone")
	@WebResult(name = "reussi")
	public boolean addPolygone(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			boolean fill,
			@WebParam(name = "xCoords", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			List<Integer> xCoords,
			@WebParam(name = "yCoords", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			List<Integer> yCoords) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

			//On trouve l'image et on effectue l'unmarshal
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			//On cr�e et on ajoute l'objet
			ArrayList<Point> points = new ArrayList<Point>();
			
			for(int i = 0; i < xCoords.size(); i++) {
				Point point = facto.createPoint(xCoords.get(i), yCoords.get(i), r, g, b);
				points.add(point);
			}
			
			Polygone poly = facto.createPolygone(x, y, r, g, b, r2, g2, b2, fill, points);
			image.getObjets().add(poly);
			
			//On marshal l'image modifi�e
			marsh.marshal(image, new File(nom + ".xml"));
			
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "addImage")
	@WebResult(name = "reussi")
	public boolean addImage(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "name", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nomImage,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int b) throws NoFileFoundException {

		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			Marshaller marsh = context.createMarshaller();
			marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));

			//On trouve l'image et on effectue l'unmarshal
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			//On ajoute l'objet
			file = new File(nomImage + ".xml");
			
			if(!file.exists()) {
				throw new NoFileFoundException("Désolé mais le fichier " + nomImage + ".xml n'existe pas.");
			}
			
			MetaImage image2 = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nomImage + ".xml"))).getValue();
			
			Image img = image2.toImage(x, y, r, g, b);
			image.getObjets().add(img);
			
			//On marshal l'image modifiée
			marsh.marshal(image, new File(nom + ".xml"));			
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	@WebMethod(operationName = "ListImages")
	@WebResult(name = "list")
	public List<String> listImages() {
		File currentFile = new File("./");
		
		String[] imageFiles = currentFile.list(new OnlyExts("xml;jpg;png;gif;bmp;wbmp"));
		ArrayList<String> list = new ArrayList<String>(imageFiles.length);
		
		for(int i = 0; i < imageFiles.length; ++i)
			list.add(i, imageFiles[i].substring(0, imageFiles[i].lastIndexOf('.')));
		
		return list;
	}
	
	@WebMethod(operationName = "getImage")
	@WebResult(name = "image", targetNamespace = "imageManager.image")
	public byte[] getImage(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom,
			@WebParam(name = "format", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String format) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			JAXBContext context = JAXBContext.newInstance("imageManager.image");
			Unmarshaller unmarsh = context.createUnmarshaller();
			
			MetaImage image = ((JAXBElement<MetaImage>) unmarsh.unmarshal(new File(nom + ".xml"))).getValue();
			
			if(!format.toLowerCase().equals("png") && !format.toLowerCase().equals("gif") && !format.toLowerCase().equals("jpg") && !format.toLowerCase().equals("bmp") && !format.toLowerCase().equals("wbmp")) {
				return null;
			}
			else {
				BufferedImage img = drawImage(image);
				ByteArrayOutputStream bas = new ByteArrayOutputStream();
				ImageIO.write(img, format, bas);
				
				return bas.toByteArray();
			}
		}
		catch (JAXBException je) {
			je.printStackTrace();
			return null;
		} 
		catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@WebMethod(operationName = "getXML")
	@WebResult(name = "XML")
	public String getXML(
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String nom) throws NoFileFoundException {
		
		File file = new File(nom + ".xml");
		if(!file.exists()) {
			throw new NoFileFoundException("Désolé mais le fichier " + nom + ".xml n'existe pas.");
		}
		
		try {
			BufferedReader reader = new BufferedReader(new FileReader(nom + ".xml"));
			String ligne, xml = "";
			
			while((ligne = reader.readLine()) != null) {
				xml += ligne;
				xml += "\n";
			}
			
			reader.close();
			
			return xml;
		}
		catch (IOException ie) {
			ie.printStackTrace();
			return null;
		}
	}
	
	@WebMethod(operationName = "clientAvailable")
	@WebResult(name = "availability")
	public boolean clientAvailable() {
		return (charge < pool.size());
	}
	
	@WebMethod(operationName = "getConnection")
	@WebResult(name = "clientID")
	public String getConnection() {
		if(clientAvailable()) {
			for(Enumeration<String> e = pool.keys(); e.hasMoreElements();) {
				String key = e.nextElement();
				if(!pool.get(key)) {
					pool.put(key, new Boolean(true));
					charge++;
					return key;
				}
			}
		}
		
		return null;
	}
	
	@WebMethod(operationName = "disconnect")
	@WebResult(name = "reussi")
	public boolean disconnect(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String clientID) {
		
		if(pool.get(clientID) != null) {
			pool.put(clientID, new Boolean(false));
			charge--;
			return true;
		}
		else
			return false;
	}
	
	/**
	 * Méthode permettant de vérifier que l'ID passée en paramètre appartient bien
	 * au pool de clients de ce serveur et que cette ID est bien utilisée.
	 * 
	 * @param ClientID L'ID que l'on veut tester.
	 * @return Vrai si l'ID est correcte, Faux sinon.
	 */
	@WebMethod(operationName = "isCorrectID")
	@WebResult(name = "correct")
	public boolean isCorrectID(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			String clientID) {
		
		if((pool.get(clientID) != null) && (pool.get(clientID) == true))
			return true;
		else
			return false;
	}
	
	@WebMethod(operationName = "getCharge")
	@WebResult(name = "charge")
	public int getCharge() {
		return charge;
	}
	
	/**
	 * Permet de créer une image Java à partir d'une {@link image.MetaImage}.
	 * 
	 * @param meta La MetaImage à dessiner.
	 * @return L'image Java dessinée.
	 * @see image.MetaImage
	 */
	private BufferedImage drawImage(MetaImage meta) {
		BufferedImage image = new BufferedImage(meta.getHauteur(), meta.getLargeur(), BufferedImage.TYPE_INT_RGB);
		
		Graphics2D g = (Graphics2D)image.getGraphics();
		
		for(Objet obj : meta.getObjets()) {
			obj.drawObjet(g);
		}
		
		return image;
	}
}
